package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.entity.Result;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.itheima.constant.MessageConstant.*;

@Service(version = "1.0",interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Resource
    OrderSettingMapper orderSettingMapper;
    @Override
    public Result save(ArrayList<OrderSetting> orderList) {
        try {
            //重复上传,只能有一个数据
            for (OrderSetting orderSetting : orderList) {
                OrderSetting db = orderSettingMapper.findOrderSettingByDate(orderSetting.getOrderDate());
                if (db==null){
                    orderSettingMapper.save(orderSetting);
                } else {
                    db.setNumber(orderSetting.getNumber());
                    orderSettingMapper.updateNumber(db);
                }
            }
            return Result.success(ORDERSETTING_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(ORDERSETTING_FAIL);
    }


//    SELECT * FROM t_ordersetting where orderDate BETWEEN "2021-11-01" AND "2021-11-31"
    @Override
    public Result findByMonth(String month) {
        try {
            String start = month+"-01";
            String end = month+"-31";

            List<OrderSetting> orderSettingList = orderSettingMapper.findOrderSettingByMonth(start,end);

            List<Map<String, Integer>> list = new ArrayList<>();

            for (OrderSetting orderSetting : orderSettingList) {
                HashMap<String, Integer> map = new HashMap<>();

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(orderSetting.getOrderDate());

                map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());

                list.add(map);
            }
            return Result.success(GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail(GET_ORDERSETTING_FAIL);
    }

    @Override
    public Result updateNumber(String date,Integer number) {
        /**
         * 根据日期进行更新,如果数据库没有,新增
         */
        try {
            Date orderDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            OrderSetting orderSetting = orderSettingMapper.findOrderSettingByDate(orderDate);
            if (orderSetting==null){
                orderSetting = new OrderSetting();
                orderSetting.setNumber(number);
                orderSetting.setOrderDate(orderDate);
                orderSetting.setReservations(0);
                orderSettingMapper.save(orderSetting);
            }else {
                orderSetting.setNumber(number);
                orderSettingMapper.updateNumber(orderSetting);
            }
            return Result.success(UPDATE_ORDERSETTING_SUCCESS,null);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Result.fail(UPDATE_ORDERSETTING_FAIL);
    }
}
